package com.ruoyi.system.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Objects;
import java.util.logging.Logger;

/**
 * @ClassName: TokenUtil
 * @Description TODO
 * @Author: lingzhi
 * @Description:
 * @Date: Created in  2024/3/30  23:07
 */
public class TokenUtil {

    public static String create(String id, String subject) {
        JwtBuilder builder = Jwts.builder()
                .setId(id)        // jwt 的唯一标识，避免重放攻击
                .setSubject(subject)
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, getSign());
        return builder.compact();
    }


    public static String create(String id, String subject, Integer second) {

        if (Objects.isNull(second)) {
            return create(id, subject);
        }
        int millisecond = second * 1000;
        Date expir = new Date(System.currentTimeMillis() + millisecond);

        JwtBuilder builder = Jwts.builder()
                .setId(id)        // jwt 的唯一标识，避免重放攻击
                .setSubject(subject)
                .setIssuedAt(new Date())
                .setExpiration(expir)
                .signWith(SignatureAlgorithm.HS256, getSign());
        return builder.compact();
    }

    public static String memberCreate(String id, String subject, Integer second) {

        if (Objects.isNull(second)) {
            return create(id, subject);
        }
        int millisecond = second * 1000;
        Date expir = new Date(System.currentTimeMillis() + millisecond);

        JwtBuilder builder = Jwts.builder()
                .setId(id)        // jwt 的唯一标识，避免重放攻击
                .setSubject(subject)
                .setIssuedAt(new Date())
                .setExpiration(expir)
                .signWith(SignatureAlgorithm.HS256, getMemberSign());
        return builder.compact();
    }


    public static Claims parse(String token) {
        return Jwts.parser().setSigningKey(getSign())
                .parseClaimsJws(token)
                .getBody();
    }

    public static String getSign() {
        return GlobalConstants.SIGN_KEY;
    }

    public static String getMemberSign() {
        return GlobalConstants.MEMBER_SIGN_KEY;
    }

    public static String getUserNameFromTokenForMember(String token) {
        String username;
        try {
            Claims claims = getClaimsFromTokenForMember(token);
            username = claims.getSubject();
        } catch (Exception e) {
            username = null;
        }
        return username;
    }

    /**
     * 从token中获取登录用户名
     */
    public static String getUserNameFromToken(String token) {
        String username;
        try {
            Claims claims = getClaimsFromToken(token);
            username = claims.getSubject();
        } catch (Exception e) {
            username = null;
        }
        return username;
    }

    /**
     * 从token中获取JWT中的负载
     */
    private static Claims getClaimsFromToken(String token) {
        Claims claims = null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(getSign())
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
          //  Logger.info("JWT格式验证失败:{}", token);

        }
        return claims;
    }
    private static Claims getClaimsFromTokenForMember(String token) {
        Claims claims = null;
        try {
            claims = Jwts.parser()
                    .setSigningKey(getMemberSign())
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            //  Logger.info("JWT格式验证失败:{}", token);

        }
        return claims;
    }

}